package task;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Util {
    public List<Equipment> getEquipment(List<Equipment> es) {
        if (null == es || es.size() == 0) {
            return null;
        }
        Equipment findE = findStartE(es);
        if (null == findE) {
            return null;
        }
        double minAngleDif, oldAngle = 2 * Math.PI;
        double oldLen = 0;
        List<Equipment> bound = new ArrayList<>(es.size());
        do {
            minAngleDif = 2 * Math.PI;
            bound.add(findE);
            Equipment nextE = findE;
            double nextAngle = oldAngle;
            for (int i = 0; i < es.size(); i++) {
                if (es.get(i).founded) {//已经被找到的点
                    continue;
                }
                if (es.get(i).point.x == findE.point.x && es.get(i).point.y == findE.point.y) {//同一个点
                    continue;
                }
                double currAngle = angleof(findE.point, es.get(i).point);
                double angleDif = reviseAngle(oldAngle - currAngle);
                if (angleDif < minAngleDif || angleDif == minAngleDif && calLen(findE.point, es.get(i).point) > oldLen) {
                    minAngleDif = angleDif;
                    nextE = es.get(i);
                    nextAngle = currAngle;
                    oldLen = calLen(findE.point, es.get(i).point);
                }
            }
            oldAngle = nextAngle;
            findE = nextE;
            findE.founded = true;
        }while (findE != bound.get(0));
        return bound;
    }

    /**
     * 寻找起始点(保证y最大的情况下、尽量使x最小的点)
     */
    public Equipment findStartE(List<Equipment> src) {
        if (null == src || src.size() == 0) {
            return null;
        }
        Equipment e = src.get(0);
        for (int i = 0; i < src.size(); i++) {
            if (src.get(i).point.getY() > e.point.getY() || src.get(i).point.getY() == e.point.getY() &&
                    src.get(i).point.getX() < e.point.getX()) {
                e = src.get(i);
            }
        }
        return e;
    }

    /**
     * 计算两点组成的向量与X轴正方向的向量角度
     */
    public static double angleof(Point a, Point b) {
        double dist = calLen(a, b);
        if (dist <= 0) {
            return 0d;
        }
        double x = b.x - a.x;
        double y = b.y - a.y;
        if (y >= 0) {//1,2象限
            return Math.acos(x / dist);
        } else {//3,4象限
            return Math.acos(-x / dist) + Math.PI;
        }
    }

    /**
     * 求两点之间的长度
     */
    public static double calLen(Point a, Point b) {
//        if (null == a || null == b) {
//            return 0d;
//        }
//        if (a == b) {
//            return 0d;
//        }
        if(a.getX() == b.getX() && a.getY() == b.getY()){
            return 0d;
        }else{
            double i = Math.abs(a.x - b.x);
            double j = Math.abs(a.y - b.y);
            double max = Math.max(i, j);
            double min = Math.min(i, j);
            double inner = min / max;
            return Math.sqrt(inner * inner + 1) * max;
        }

    }


    /**
     * 修正角度到【0,2PI】
     *
     * @param angle
     * @return
     */
    private static double reviseAngle(double angle) {
        while (angle < 0.) {
            angle += 2 * Math.PI;
        }
        while (angle >= 2 * Math.PI) {
            angle -= 2 * Math.PI;
        }
        return angle;
    }
    public static void findMinDis(Worker a,List<Equipment> equipments){
        double min = 0d;
        int j = 0;
        double[] v = new double[equipments.size()];
        for (int i = 0; i < equipments.size(); i++) {

            v[i] = Util.calLen(a.point , equipments.get(i).point);
            min = v[0];
            if (min > v[i]){
                min = v[i];
                j = i;
            }
        }
        System.out.println(a.getName()  + "距离" + equipments.get(j).getName() +"[" + equipments.get(j).point.x+ "," + equipments.get(j).point.y+ "]最近." );
    }
}

